import * as CommodityServer from '../service/commodity'

export default {
    namespace: 'commodity',
    state: {
        list: [],
        map: {}
    },
    reducers: {
        all(state, { payload }) {
            let list = []
            let map = {}
            if (Array.isArray(payload)) {
                payload.forEach(item => {
                    list.push(item)
                    map[item._id] = item
                })
            }
            return {
                ...state,
                list,
                map
            }
        }
    },
    effects: {
        *getAll({}, { call, put }) {
            let res = yield call(CommodityServer.getAll)
            yield put({ type: 'all', payload: res.data })
        }
    }
}
